'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.rdf.RDFWriterI import RDFWriterI
from base.rdf.impl.RDFDefaultErrorHandler import RDFDefaultErrorHandler
from base.shared.JenaException import JenaException

#import java.io.*;
#import java.util.Locale;

#import org.apache.jena.rdf.model.*;
#import org.apache.jena.shared.*;
#import org.apache.jena.util.FileUtils;
#import org.slf4j.Logger;
#import org.slf4j.LoggerFactory;

'''
 * Writes out an XML serialization of a model.
'''



class NTripleWriter(RDFWriterI):

    # RDFErrorHandler errorHandler = RDFDefaultErrorHandler()

    # param NTripleWriter.class
    # result static Logger logger = LoggerFactory.getLogger(self)

    # param 
    def __init__(self):
        super().__init__()

    # @Override
    # param Model model, OutputStream out, String base
    # result void
    def write(self, model, out, base):
        pass
        # try:
        #     try:
        #         w = OutputStreamWriter(out, "ascii")
        #     catch (UnsupportedEncodingException e):
        #         logger.warn("ASCII is not supported: in NTripleWriter.write", e)
        #         w = FileUtils.asUTF8(out)
        #
        #     write(model, w, base)
        #     w.flush()
        #
        # catch (Exception ioe):
        #     errorHandler.error(ioe)
        
    

    # @Override
    # param Model model, Writer writer, String base
    # result void
    def write(self, model, writer, base):
        pass
        # try:
        #     PrintWriter pw
        #     if (writer instanceof PrintWriter):
        #         pw = (PrintWriter) writer
        #     else:
        #         pw = PrintWriter(writer)
        #
        #
        #     StmtIterator ite = model.listStatements()
        #     Statement stmt = null
        #
        #     while (ite.hasNext()):
        #         stmt = ite.nextStatement()
        #         writeResource(stmt.getSubject(), pw)
        #         pw.print(" ")
        #         writeResource(stmt.getPredicate(), pw)
        #         pw.print(" ")
        #         writeNode(stmt.getObject(), pw)
        #         pw.println(" .")
        #
        #     pw.flush()
        # catch (Exception e):
        #     errorHandler.error(e)
        
    

    '''
     * Set a property to control the writer's behaviour.
     *
     * <p>This writer currently recognises no properties.  Invoking self
     * method always causes an <CODE>UnknownPropertyException</CODE>
     * to be raised.</p>?
     *
     * @param propName  The name of the property to be set
     * @param propValue The value of the property
     * @return the previous value of the property
    '''
    # @Override
    # param String propName, Object propValue
    # result Object
    def setProperty(self, propName, propValue):
        raise JenaException(propName)
    

    # param String prefix, String ns
    # result void
    def setNsPrefix(self, prefix, ns):
        pass
    

    # param String uri
    # result String
    def getPrefixFor(self, uri):
        return None
    

    # @Override
    # param RDFErrorHandler errHandler
    # result RDFErrorHandler
    def setErrorHandler(self, errHandler):
        pass
        # RDFErrorHandler old = self.errorHandler
        # self.errorHandler = errHandler
        # return old
    

    # param Model model, PrintWriter writer
    # result static void
    # def write(self, model, writer):
    #     StmtIterator ite = model.listStatements()
    #     Statement stmt = null
    #
    #     while (ite.hasNext()):
    #         stmt = ite.nextStatement()
    #         writeResource(stmt.getSubject(), writer)
    #         writer.print(" ")
    #         writeResource(stmt.getPredicate(), writer)
    #         writer.print(" ")
    #         writeNode(stmt.getObject(), writer)
    #         writer.println(" .")
        
    

    # param Resource r, PrintWriter writer
    # result static void
    # def writeResource(self, r, writer):
    #     if (r.isAnon()):
    #         writer.print(anonName(r.getId()))
    #     else:
    #         writer.print("<")
    #         writeURIString(r.getURI(), writer)
    #         writer.print(">")
        
    

    # static private boolean okURIChars[] = boolean[128]

    # static:
    #     for (int i = 32 i < 127 i++)
    #         okURIChars[i] = True
    #     okURIChars['<'] = False
    #     okURIChars['>'] = False
    #     okURIChars['\\'] = False

    

    # param String s, PrintWriter writer
    # result static void
    # def writeURIString(self, s, writer):
    #
    #     for (int i = 0 i < s.length() i++):
    #         char c = s.charAt(i)
    #         if (c < okURIChars.length and okURIChars[c]):
    #             writer.print(c)
    #         else:
    #             String hexstr = Integer.toHexString(c).toUpperCase(Locale.ENGLISH)
    #             int pad = 4 - hexstr.length()
    #             writer.print("\\u")
    #             for ( pad > 0 pad--)
    #                 writer.print("0")
    #             writer.print(hexstr)
            
        
    

    # param String s, PrintWriter writer
    # result static void
    # def writeString(self, s, writer):
    #
    #     for (int i = 0 i < s.length() i++):
    #         char c = s.charAt(i)
    #         if (c == '\\' or c == '"'):
    #             writer.print('\\')
    #             writer.print(c)
    #         elif (c == '\n'):
    #             writer.print("\\n")
    #         elif (c == '\r'):
    #             writer.print("\\r")
    #         elif (c == '\t'):
    #             writer.print("\\t")
    #         elif (c >= 32 and c < 127):
    #             writer.print(c)
    #         else:
    #             String hexstr = Integer.toHexString(c).toUpperCase()
    #             int pad = 4 - hexstr.length()
    #             writer.print("\\u")
    #             for ( pad > 0 pad--)
    #                 writer.print("0")
    #             writer.print(hexstr)
            
        
    

    # param Literal l, PrintWriter writer
    # result static void
    # def writeLiteral(self, l, writer):
    #     String s = l.getString()
    #     '''
    #     if (l.getWellFormed()):
    #     	writer.print("xml")
    #    '''
    #     writer.print('"')
    #     writeString(s, writer)
    #     writer.print('"')
    #     String lang = l.getLanguage()
    #     if (lang is not None and !lang.equals("")):
    #         writer.print("@" + lang)
    #     String dt = l.getDatatypeURI()
    #     if (dt is not None and !dt.equals("")):
    #         writer.print("^^<" + dt + ">")
    

    # param RDFNode n, PrintWriter writer
    # result static void
    # def writeNode(self, n, writer):
    #     if (n instanceof Literal):
    #         writeLiteral((Literal) n, writer)
    #     else:
    #         writeResource((Resource) n, writer)
    #
    

    # param AnonId id
    # result static String
    # def anonName(self, id):
    #     String name = "_:A"
    #     String sid = id.toString()
    #     for (int i = 0 i < sid.length() i++):
    #         char c = sid.charAt(i)
    #         if (c == 'X'):
    #             name = name + "XX"
    #         elif (Character.isLetterOrDigit(c)):
    #             name = name + c
    #         else:
    #             name = name + "X" + Integer.toHexString(c) + "X"
    #
    #
    #     return name
    

